package com.xiaohua.tlgorders.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xiaohua.tlgorders.entity.Order;
import com.xiaohua.tlgorders.entity.OrderGoods;
import com.xiaohua.tlgorders.mapper.OrderGoodsMapper;
import com.xiaohua.tlgorders.service.OrderGoodsService;
import com.xiaohua.tlgorders.tools.Record;
import com.xiaohua.tlgorders.tools.Status;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 *@Author 小花
 *@Date 2019/7/16 9:43
 **/
@Service
public class OrderGoodsServiceImpl implements OrderGoodsService {
    @Autowired
    OrderGoodsMapper mapper;
    @Autowired
    RedisTemplate redisTemplate;
    @Transactional
    @Override
    /*根据用户ID查询订单*/
    public Record findUesrId(Integer userId,Integer page,Integer size) {
        Record record=new Record();
    try {
//            String key="UserOrderId_"+userId;        //根据用户Id定义redis钥匙名
//
//         ListOperations<String, PageInfo> operations = redisTemplate.opsForList();
////              if(redisTemplate.hasKey(key)){
//                   record.setStatus(Status.SELECT_SUCCESS);
//                  record.setData(operations.range(key, 0, -1));
//                  record.setMsg("用户ID查询订单");
//                  return record;
//                }else{




//                                record.setStatus(Status.SELECT_FALIED);
//                                record.setData(null);
//                                record.setMsg("内容为空");
        PageHelper.startPage(page,size);
        List<Order> list=mapper.findUesrId(userId);
        for (int i = 0; i <list.size() ; i++) {

            for (int j = i+1; j < list.size() ; j++) {
                if(list.get(i).getOrderNo().equals(list.get(j).getOrderNo())){


                    for (int k = 0; k <list.get(j).getOrderGoods().size() ; k++) {
                        OrderGoods goods=   list.get(j).getOrderGoods().get(k);
                        list.get(i).getOrderGoods().add(goods);
                        System.out.println(list.get(i).getOrderGoods().size());
                        System.out.println(list.get(j).getOrderGoods().size());
                    }

                    list.remove(j);

                }

            }
        }
                                PageInfo<Order> goodsPageInfo=new PageInfo<>(list);

//                                operations.leftPushAll(key, goodsPageInfo);
                                record.setStatus(Status.SELECT_SUCCESS);
                                record.setData(goodsPageInfo);
                                record.setMsg("根据用户查询订单");







   }catch (Exception e){
               record.setData(null);
              record.setMsg("查询错误"+e);
               record.setStatus(Status.SELECT_FALIED);
   }finally {
       return record;
    }

    }
    @Transactional
    @Override
    public Record findDetail(Integer userId,String orderNo,Integer addressId,Integer page,Integer size){
        Record record=new Record();
        record.setData(mapper.findDetail(userId,orderNo,addressId));
        if(record.getData()!=null){
            record.setMsg("查看商品详情");
            record.setStatus(Status.SELECT_SUCCESS);
        }else{
            record.setMsg("查询失败");
            record.setMsg(Status.SELECT_FALIED);
        }
        return  record;
    }
    @Transactional

    public Record findByNoOrName(String orderNo, String goodsName, Integer userId,Integer page,Integer size) {
//        ListOperations<String, PageInfo> operations = redisTemplate.opsForList();
        Record record=new Record();
        try {
//            String key="NameOrId_"+orderNo;        //根据用户Id定义redis钥匙名

//            if(redisTemplate.hasKey(key)){
//                record.setStatus(Status.SELECT_SUCCESS);
//                    record.setData(operations.range(key, 0, -1));
//                record.setMsg("商品名或订单号查询");
//                return record;
//            }else{

                PageHelper.startPage(page,size);

            List<Order> list=mapper.findByNoOrName(orderNo, goodsName, userId);
            for (int i = 0; i <list.size() ; i++) {

                for (int j = i+1; j < list.size() ; j++) {
                    if(list.get(i).getOrderNo().equals(list.get(j).getOrderNo())){


                        for (int k = 0; k <list.get(j).getOrderGoods().size() ; k++) {
                            OrderGoods goods=  list.get(j).getOrderGoods().get(k);
                            list.get(i).getOrderGoods().add(goods);
                            System.out.println(list.get(i).getOrderGoods().size());
                            System.out.println(list.get(j).getOrderGoods().size());
                        }

                        list.remove(j);

                        }

                }
            }

            PageInfo<Order> goodsPageInfo=new PageInfo<>(list);
//                operations.leftPushAll(key, goodsPageInfo);
                record.setStatus(Status.SELECT_SUCCESS);
                record.setData(goodsPageInfo);
                record.setMsg("商品名或订单号查询");
//                return record;
//            }


        }catch (Exception e){
            record.setData(null);
            record.setMsg("查询错误"+e);
            record.setStatus(Status.SELECT_FALIED);
        }finally {
            return record;
        }
    }
    @Transactional
    @Override
    public Record findStatus(Integer userId, Integer orderStatus, Integer page, Integer size) {
        Record record=new Record();
        try {
//            String key="orderStatus_"+orderStatus;        //根据用户Id定义redis钥匙名
//
//            ListOperations<String, PageInfo> operations = redisTemplate.opsForList();
            PageHelper.startPage(page,size);
            List<Order> list=mapper.findStatus(userId,orderStatus);

            for (int i = 0; i <list.size() ; i++) {

                for (int j = i+1; j < list.size() ; j++) {
                    if(list.get(i).getOrderNo().equals(list.get(j).getOrderNo())){


                        for (int k = 0; k <list.get(j).getOrderGoods().size() ; k++) {
                            OrderGoods goods= list.get(j).getOrderGoods().get(k);
                            list.get(i).getOrderGoods().add(goods);
                        }

                        list.remove(j);

                    }

                }
            }
            PageInfo<Order> goodsPageInfo=new PageInfo<>(list);

//            if(redisTemplate.hasKey(key)){
//                record.setStatus(Status.SELECT_SUCCESS);
//                record.setData(operations.range(key, 0, -1));
//                record.setMsg("订单状态查询");
//                return record;
//            }else{


//                operations.leftPushAll(key, goodsPageInfo);
                record.setStatus(Status.SELECT_SUCCESS);
                record.setData(goodsPageInfo);
                record.setMsg("订单状态查询");
                return record;
//            }


        }catch (Exception e){
            record.setData(null);
            record.setMsg("查询错误"+e);
            record.setStatus(Status.SELECT_FALIED);
        }finally {
            return record;
        }
    }
    @Transactional
    @Override
    public Record byNameOrNoCount(String orderNo, String goodsName, Integer userId) {
        Record record=new Record();
        try {
                record.setMsg("查询订单数量");
                record.setData(mapper.byNameOrNoCount(orderNo,goodsName,userId));
                record.setStatus(Status.SELECT_SUCCESS);
        }catch (Exception e){
            record.setMsg("查询出错");
            record.setData(e);
            record.setStatus(Status.SELECT_FALIED);
        }finally {
            return record;
        }

    }


}
